#include "SclXWriter.h"
#include "Scene.h"
#include "Node.h"

SclXWriter::SclXWriter(Scene* scene) : scene(scene) {
     xml.setAutoFormatting(true);
}

bool SclXWriter::writeFile(QIODevice *device) {
	xml.setDevice(device);

	xml.writeStartDocument();
	xml.writeDTD("<!DOCTYPE scl>");
	writeNode(scene->getRoot());
	xml.writeEndDocument();
	return true;
}

void SclXWriter::writeNode(const Node *node) {
	xml.writeStartElement(node->typeName());
	xml.writeAttribute("name", node->name);
	xml.writeAttribute("x", QString::number(node->x()));
	xml.writeAttribute("y", QString::number(node->y()));
	for(QHash<QString, QVariant>::ConstIterator it = node->attr.begin(); it != node->attr.end(); ++it) {
		xml.writeEmptyElement("attribute");
		xml.writeAttribute("key", it.key());
		xml.writeAttribute("value", it.value().toString());
	}
	foreach(const Node* ch, node->getChildren()) {
		writeNode(ch);
	}
	xml.writeEndElement();
}
